/*
 * Copyright 2008-2010 Maxim Starodubcev <starodubcevmax@gmail.com>
 * Copyright 2009-2010 Sergey Rudchenko <rudchenkos@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "ffilter.h"
#include "circuit.h"

#include <math.h>
#include <stdio.h>
#include <locale.h>
#include <libintl.h>

#define _(x) gettext(x)

void calculate_filter_interactive(int filter_type)
{
    if (filter_type == LOW_FREQ || filter_type == HIGH_FREQ)
    {
        double f, r, L, C;
        int z;

        printf(_("Enter cut-off frequency in kHz, or 'q' to exit\n:"));
        scanf("%lf", &f);

        printf(_("Enter load resistance in ohms, or 'q' to exit\n:"));
        scanf("%lf", &r);

        printf(_("Enter the type of topology, or 'q' to exit\n1:L\n2:T\n3:Pi\n:"));
        scanf("%d", &z);

        if (filter_type == LOW_FREQ)
        {
            L = (0.32 * r) / f;
            C = 320 / (f * r);
            switch (z)
            {
            case 1:
                C = C / 2.0;
                L = L / 2.0;
                break;
            case 2:
                L = L / 2.0;
                break;
            case 3:
                C = C / 2.0;
                break;
            }
        }  

        if (filter_type == HIGH_FREQ)
        {
            L = (0.08 * r) / f;
            C = 80 / (r * f);
            switch (z)
            {
            case 1:
                C = C * 2.0;
                L = L * 2.0;
                break;
            case 2:
                C = C * 2.0;
                break;
            case 3:
                L = L * 2.0;
                break;
            }
        }

        print_filter_circuit(filter_type, z);
        printf("L=%10.3lf mH\nC=%10.3lf uF\n", L, C);
    }

    if (filter_type == BANDPASS || filter_type == BAND_REJECT)
    {
        double f1, f2, r, L1, L2, C1, C2;
        int z;
	    
        printf(_("Enter the first cut-off frequency in kHz, or 'q' to exit\n:"));
        scanf("%lf", &f1);

        printf(_("Enter the second cut-off frequency in kHz, or 'q' to exit\n:"));
        scanf("%lf", &f2);

        printf(_("Enter load resistance in ohms, or 'q' to exit\n:"));
        scanf("%lf", &r);

        if (filter_type == BANDPASS)
        {
            L1 = (0.32 * r) / fabs(f2 - f1);
            L2 = (0.08 * fabs(f2 - f1) * r) / (f1 * f2);
            C1 = (80 * fabs(f2 - f1)) / (r * f1 * f2);
            C2 = 320 / (r * fabs(f2 - f1));
        }

        if (filter_type == BAND_REJECT)
        {
            L1 = (0.32 * r * fabs(f2 - f1)) / (f1 * f2);
            L2 = (0.08 * r) / fabs(f2 - f1);
            C1 = 80 / (fabs(f2 - f1) * r);
            C2 = (320 * fabs(f2 - f1)) / (r * f1 * f2);
        }
	    
        printf(_("Enter the type of topology, or 'q' to exit\n1:L\n2:T\n3:Pi\n:"));
        scanf("%d", &z);

        if (z == 1 && filter_type == BANDPASS)
        {
            printf("    L1      C1                 \n");
            printf("o---^^^-----||------+---------o\n");
            printf("                  __|_         \n");
            printf("                 |    )        \n");
            printf("            C2  ===   ) L2     \n");
            printf("                 |____)        \n");
            printf("                    |          \n");
            printf("o-------------------+---------o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1 / 2,L2 * 2,C1 * 2,C2 / 2);
        }

        if (z == 2 && filter_type == BANDPASS)
        {
            printf("    L1    C1       C1   L1     \n");
            printf("o---^^^---||---+---||---^^^---o\n");
            printf("             __|_              \n");
            printf("            |    )             \n");
            printf("       C2  ===   ) L2          \n");
            printf("            |____)             \n");
            printf("               |               \n");
            printf("o--------------+--------------o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1 / 2,L2,C1 * 2,C2);
        }

        if (z == 3 && filter_type == BANDPASS)
        {
            printf("            L1    C1           \n");
            printf("o------+----^^^---||----+-----o\n");
            printf("     __|_             __|_     \n");
            printf("    |    )           |    )    \n");
            printf("C2 ===   ) L2    C2 ===   ) L2 \n");
            printf("    |____)           |____)    \n");
            printf("       |                |      \n");
            printf("o------+----------------+-----o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1,L2 * 2,C1,C2 / 2);
        }

        if (z == 1 && filter_type == BAND_REJECT)
        {   
            printf("       L1                           \n");
            printf("    +--^^^--+                       \n");
            printf("o---|       |---------+------------o\n");
            printf("    +--||---+         |             \n");
            printf("       C1             )             \n");
            printf("                      )  L2         \n");
            printf("                      )             \n");
            printf("                      |             \n");
            printf("                     === C2         \n");
            printf("                      |             \n");
            printf("o---------------------+------------o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1 / 2,L2 * 2,C1 * 2,C2 / 2);
        }

        if (z == 2 && filter_type == BAND_REJECT)
        {
            printf("       L1                   L1        \n");
            printf("    +--^^^--+            +--^^^--+    \n");
            printf("o---|       |-----+------|       |---o\n");
            printf("    +--||---+     |      +--||---+    \n");
            printf("       C1         )         C1        \n");
            printf("                  )  L2               \n");
            printf("                  )                   \n");
            printf("                  |                   \n");
            printf("                 === C2               \n");
            printf("                  |                   \n");
            printf("o-----------------+------------------o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1 / 2,L2,C1 * 2,C2);
        }

        if (z == 3 && filter_type == BAND_REJECT)
        {
            printf("                L1                   \n");
            printf("             +--^^^--+               \n");
            printf("o------+-----|       |-----+--------o\n");
            printf("       |     +--||---+     |         \n");
            printf("       )        C1         )         \n");
            printf("       ) L2                )  L2     \n");
            printf("       )                   )         \n");
            printf("       |                   |         \n");
            printf("      === C2              === C2     \n");
            printf("       |                   |         \n");
            printf("o------+-------------------+--------o\n");
            printf("L1=%10.3lf mH\nL2=%10.3lf mH\nC1=%10.3lf uF\nC2=%10.3lf uF\n",L1,L2 * 2,C1,C2 / 2);
        }
    }
}

int main()
{
    setlocale(LC_ALL, "");
    bindtextdomain("ffilter", INSTALL_PREFIX "/locale");
    textdomain("ffilter");

    printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    printf(_("+ This is a program for calculating the electronic filters.+\n"));
    printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");  
 
    while (1)
    {
	printf(_("Enter the type of filter, or 'q' to exit\n1:LP\n2:HP\n3:BANDPASS\n4:BAND-REJECT\n:"));
        char key;
        do
        {
            key = getchar();
        }
        while (key == '\n'); // skip newlines

        if (key == 'q' || key == EOF)
            break;

        int type = key - '0';
        calculate_filter_interactive(type);
    }

    return 0;
}
